﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Battles.Atomics;

namespace Battles.Controls
{
	public delegate void ArmyCreated (Army army);

	public partial class ArmyControl : UserControl
	{
		Army army = new Army();

		#region - Properties. -
		/// <summary>
		/// Warlord's name.
		/// </summary>
		public string Warlord
		{
			get { return WarlordBox.Text; }
			set { WarlordBox.Text = value; }
		}

		public Army Army
		{ 
			get
			{
				return army;
			}
			set
			{
				army = value;
				ArchersNumeric.Value = army.Archers.Count;
				CavalryNumeric.Value = army.Cavalry.Count;
				LightInfNumeric.Value = army.LightInf.Count;
				HeavyInfNumeric.Value = army.HeavyInf.Count;

				StrengthBox.Text = BattleEngine.BattleEngine.GetArmyStrength(army).ToString();
				HealthBox.Text = Utils.GetTotalHealth(value.Warriors).ToString();
			} 
		}

		public Atomics.ArmyPlacement Placement { get; set; }
		#endregion

		#region - Event. -
		public event ArmyCreated OnArmyCreated; 
		#endregion

		public ArmyControl()
		{
			InitializeComponent();
		}

		#region - Event handlers. -
		/// <summary>
		/// Creates random army using the prescribed preset.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void RandomBtn_Click(object sender, EventArgs e)
		{
			Army = Army.Create((int)ArmySizeNumeric.Value, ArmyPreset.Random);
			if (OnArmyCreated != null) { OnArmyCreated(Army); }
		}

		private void PlacementBtn_Click(object sender, EventArgs e)
		{
			if (army == null) return;

			var plForm = new Form();
			plForm.Closing += PlacementFormClosing;

			var plCtrl = new ArmyPlacement();
			plCtrl.Army = army.Warriors;
			plForm.Controls.Add(plCtrl);
			plForm.AutoSize = true;

			plForm.ShowDialog();
		} 
		#endregion

		#region - Delegate event hanlders. -
		public void PlacementFormClosing(object sender, EventArgs e)
		{
			Placement = ((ArmyPlacement)((Form)sender).Controls[0]).Placement;
		}
		#endregion
	}
}